package com.xsw.edu.web.controller;

import com.xsw.edu.model.Student;
import com.xsw.edu.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * Created by lenovo on 2019/7/3.
 */
@Controller
public class StudentController {

    @Autowired
    private StudentService studentService;

    @RequestMapping("allStudents")
    public String allStudents(Model model){
        List<Student> list = studentService.findAllStudent();
        model.addAttribute("list",list);


        return "all-students";
    }

    @RequestMapping("addStudent")
    public String addStudent(){
        return "add-student";
    }

    @RequestMapping("addStudents")
    public String addStudents(Student student, Model model) throws IOException {
        System.out.println("controller student："+student);

        //保存数据库的路径
        String sqlPath = null;
        //定义文件保存的本地路径
        String localPath="E:\\www\\images\\";
        //String localPath="http:\\114.67.73.152\\img\\images\\";
        //定义 文件名
        String filename=null;
        if(!student.getFile().isEmpty()){
            //生成uuid作为文件名称
            String uuid = UUID.randomUUID().toString().replaceAll("-","");
            //获得文件类型（可以判断如果不是图片，禁止上传）
            String contentType=student.getFile().getContentType();
            //获得文件后缀名
            String suffixName=contentType.substring(contentType.indexOf("/")+1);
            //得到 文件名
            filename=uuid+"."+suffixName;
            //文件保存路径
            student.getFile().transferTo(new File(localPath+filename));
        }
        //把图片的相对路径保存至数据库
        sqlPath = "http://114.67.73.152/img/images/"+filename;
        System.out.println(sqlPath);
        student.setImageico(sqlPath);
        studentService.insertStudent(student);
        model.addAttribute("student", student);

        System.out.println("插入后   controller student："+student);

        System.out.println("添加成功===============");

        return "all-students";
    }

    //根据id查询
    @RequestMapping(value = "profile",method= RequestMethod.GET,params="method=one")
    public String profile(@RequestParam(value = "id") int id,Model model){
        Student student = studentService.findById(id);
        System.out.println("学生信息："+student);
        model.addAttribute("list",student);
        //return String.format("post %d", id);
        return "student-profile";
    }

    @RequestMapping("profile/{id}")
    public String page(Model model , @PathVariable("id") int id) {
        Student student = studentService.findById(id);
        model.addAttribute("student",student);
        return "student-profile";
    }
//    @RequestMapping(value = "/list",method= RequestMethod.GET)  
//    public String getAll(Model model) {  
//            Set<GroupSecure> secures= redisDao.get();  
//            model.addAttribute("secures", secures);  
//            return "group/list";  
//        }  
//                      
//    @RequestMapping(value = "/list",method=RequestMethod.GET,params="method=one")  
//    public String getOneUser(@RequestParam(value ="name") String name, Model model) {  
//            Set<GroupSecure> secures=redisDao.getByName(name);  
//            model.addAttribute("secures", secures);  
//            return "group/list";  
//        }  


    //简介student-profile
    @RequestMapping("stu_profile")
    public String studentProfile(Model model){
        List<Student> list = studentService.findAllStudent();
        model.addAttribute("list",list);
        return "student-profile";
    }


}
